﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaskManageDemo.Core.PagedExend;
using TaskManageDemo.Data;
using TaskManageDemo.Data.Entitys;
using TaskManageDemo.Web.Models;

namespace TaskManageDemo.Web.Controllers
{
    public class TaskController : Controller
    {
        private readonly IRepository _scheduleTask;
        public TaskController(IRepository scheduleTask)
        {
            this._scheduleTask = scheduleTask;
        }
        //
        // GET: /Task/
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAllTask(int limit, int offset)
        {
            var tasks = _scheduleTask.GetAll<ScheduleTask>().OrderByDescending(a => a.Allowused);
            var total = tasks.ToList().Count;
            var rows = tasks.Skip(offset).Take(limit).ToList();
            return Json(new { total = tasks.Count(), rows = rows }, JsonRequestBehavior.AllowGet);

        }
        [HttpPost]
        public JsonResult AddOrEditTask(ScheduleTask task)
        {
            string sql = "select * from ScheduleTask where id=@id";
            var data = string.Empty;
            try
            {
                var task2 = _scheduleTask.GetFirst<ScheduleTask>(sql, new { id = task.Id });
                if (task2 == null)
                {
                    //add
                    task.CreateTime = DateTime.Now.ToString();
                    if (_scheduleTask.Insert<ScheduleTask>(task) > 0)
                        data = "新增作业成功";
                    else
                        data = "新增作业失败";
                }
                else
                {
                    //update
                    task2.CronExpression = task.CronExpression;
                    task2.ClassName = task.ClassName;
                    task2.Remark = task.Remark;
                    task2.TaskName = task.TaskName;

                    if (_scheduleTask.Update<ScheduleTask>(task2))
                        data = "更新作业成功";
                    else
                        data = "更新作业失败";
                }
            }
            catch (Exception ex)
            {
                TaskManageDemo.Utility.Log.Logger.Error(string.Format("新增作业异常{0}", ex.ToString()));
                data = "系统异常";
            }
            return Json(data);
        }

        public JsonResult DeleteTask(string ids)
        {
            string data = string.Empty;
            try
            {
                var idArray = ids.Split(',');
                string ids2 = string.Join("','", idArray);
                string sql = string.Format("delete ScheduleTask where id in ('{0}')", ids2);
                if ((int)_scheduleTask.ExecuteNonQuery(sql) > 0)
                    data = "删除作业成功";
                else
                    data = "删除作业失败";
            }
            catch (Exception ex)
            {

                TaskManageDemo.Utility.Log.Logger.Error(string.Format("删除作业异常{0}", ex.ToString()));
                data = "系统异常";
            }
            return Json(data);
        }

        public JsonResult StatrTask(string ids)
        {
            string data = string.Empty;
            try
            {
               var idArray=ids.Split(',');
               string ids2= string.Join("','",idArray);
               string sql = string.Format("update ScheduleTask set Allowused=1 where id in ('{0}')",ids2);
               if ((int)_scheduleTask.ExecuteNonQuery(sql) > 0)
                   data = "启动作业成功";
               else
                   data = "启动作业失败";
            }
            catch (Exception ex)
            {

                TaskManageDemo.Utility.Log.Logger.Error(string.Format("启动作业异常{0}", ex.ToString()));
                data = "系统异常";
            }
            return Json(data);
        }

        public JsonResult StopTask(string ids)
        {
            string data = string.Empty;
            try
            {
               var idArray=ids.Split(',');
               string ids2= string.Join("','",idArray);
               string sql = string.Format("update ScheduleTask set Allowused=0 where id in ('{0}')",ids2);
               if ((int)_scheduleTask.ExecuteNonQuery(sql) > 0)
                   data = "暂停作业成功";
               else
                   data = "暂停作业失败";
            }
            catch (Exception ex)
            {

                TaskManageDemo.Utility.Log.Logger.Error(string.Format("暂停作业异常{0}", ex.ToString()));
                data = "系统异常";
            }
            return Json(data);
        }

    }
}